using UnityEngine;
using QFramework;
using Unity.VisualScripting;
using QAssetBundle;

// 1.请在菜单 编辑器扩展/Namespace Settings 里设置命名空间
// 2.命名空间更改后，生成代码之后，需要把逻辑代码文件（非 Designer）的命名空间手动更改
namespace QFramework.Example
{
	public partial class MiniMenemy : ViewController, IEnemy
	{
		public enum States
		{
			FlowingPlayer,
			Warning,//境界状态
			Desh,//冲向主角
			Wait,//等待
		}

		[Header("怪物移动速度")]
		[SerializeField]
		float float_EnemyMoveSpeed = 2;

		[Header("怪物生命值")]
		[SerializeField]
		float float_Hp = 3;

		FSM<States> FSM = new FSM<States>();

		void Start()
		{
			
			Global.EnemyCount.Value++;
			// Code Here

			FSM.State(States.FlowingPlayer)
			.OnFixedUpdate(() =>
			{
				if (Player.Default)
				{

					Vector2 direction = (Player.Default.transform.position - transform.position).normalized;

					SelfRigidbody2D.velocity = direction * float_EnemyMoveSpeed;

					if ((Player.Default.transform.position - transform.position).magnitude <= 8)
					{
						FSM.ChangeState(States.Warning);
					}
				}
				else
				{
					SelfRigidbody2D.velocity = Vector2.zero;
				}
			});

			FSM.State(States.Warning)
			.OnEnter(() =>
			{
				
				SelfRigidbody2D.velocity = Vector2.zero;
			})
			.OnUpdate(() =>
			{
				float frame = 1 + (60 * 1 - FSM.FrameCountOfCurrentState) / 10;
				if (FSM.FrameCountOfCurrentState / frame % 2 == 0)
				{
					
					Sprite.color = Color.red;
				}
				else
				{
					
					Sprite.color = Color.white;
				}
				if (FSM.FrameCountOfCurrentState >= 60 * 1)
				{
					FSM.ChangeState(States.Desh);
				}
			})
			.OnExit(()=>{
				Sprite.color = Color.white;
			});

			FSM.State(States.Desh)
			.OnEnter(() =>
			{

				var dir = (Player.Default.Position() - transform.Position()).normalized;
				SelfRigidbody2D.velocity = dir * 10;
			})
			.OnUpdate(() =>
			{
				if (FSM.FrameCountOfCurrentState >= 60 * 3)
				{
					FSM.ChangeState(States.Wait);
				}
			});

			FSM.State(States.Wait)
				.OnEnter(() =>
				{
					SelfRigidbody2D.velocity = Vector2.zero;
				})
				.OnUpdate(() =>
				{
					if (FSM.FrameCountOfCurrentState >= 30)
					{
						FSM.ChangeState(States.FlowingPlayer);
					}
				});


			FSM.StartState(States.FlowingPlayer);

		}

		void Update()
		{
			FSM.Update();
		}

		private void FixedUpdate()
		{
			FSM.FixedUpdate();
		}
		bool bool_Hurt = false;
		public void OnSetHp(float hurt)
		{

			if (!bool_Hurt)
			{
				var floattingText = FindObjectOfType<FloattingTextController>();
				floattingText.OnPlay(transform.position + transform.up * 0.5f, Mathf.Abs(hurt).ToString());
				AudioKit.PlaySound(Sfx.HIT);
				this.Sprite.color = Color.red;
				bool_Hurt = true;
				//延迟函数 
				ActionKit.Delay(0.3f, () =>
				{
					if (this != null)
						this.Sprite.color = Color.white;

					float_Hp += hurt;
					bool_Hurt = false;
				}).Start(this);//全局应用
			}
			if (float_Hp <= 0)
			{
				OnDieEnmey();
			}
		}

		public void OnDieEnmey()
		{
			AudioKit.PlaySound(Sfx.ENEMYDIE);
			Global.OnDropObject(gameObject);
			gameObject.DestroySelfGracefully();
		}

		/// <summary>
		/// 获取当前怪物血量
		/// </summary>
		/// <returns></returns>
		public float OnGetCurrentEnemyHp()
		{
			return float_Hp;
		}

		private void OnDestroy()
		{
			Global.EnemyCount.Value--;
		}

        public void OnSetHpScale(float hpScale)
        {
            float_Hp *= hpScale;
        }

        public void OnSetSpeedScale(float speedScale)
        {
            float_EnemyMoveSpeed *= speedScale;
        }
    }
}
